let fs = require('fs')

let loadCmds = function (loadPath) {
    try {
        var cmds = fs.readFileSync(loadPath, 'utf-8').split('\n')
                .filter(v => v.replace(/\ +|\r|\n/g, '') !== '')    //过滤空行
                .filter(v => v.replace(/\ +|\r|\n/g, '')[0] !== '#') 
    } catch (error) {
        console.error(error.message)
        var cmds = []
    }
    return function(addSudo = true) {
        return cmds
        .map(v => !addSudo || 
            v.replace(/(^\s*)|(\s*$)/g, '').slice(0, 2) === 'cd' ? v:
            'sudo ' + v)
        .map(v => !addSudo ? v.split(' ').filter(ele => ele.replace(/\ +|\r|/g, '') !== 'sudo').join(' ') : v)
    }
} 

module.exports = loadCmds

if (require.main === module) {
    console.log(program.chdir)
}